package cn.flkj.lucia2coudb.service.impl;

import cn.flkj.lucia2coudb.entity.Sofa;
import org.ektorp.CouchDbConnector;
import org.springframework.stereotype.Component;
import cn.flkj.lucia2coudb.service.CouchdbCRUD;

/**
 * @author : 州长在手 2020/10/21 下午3:32
 */
@Component
public class CouchdbCRUDImpl implements CouchdbCRUD {

    private final CouchDbConnector connector;

    public CouchdbCRUDImpl(CouchDbConnector connector) {
        this.connector = connector;
    }

    /**
     * 新增沙发
     * @param sofa
     */
    @Override
    public String newSofa(Sofa sofa){
        connector.create(sofa.getId(), sofa);
        return sofa.getId();
    }

    /**
     * 查询文档
     * @param id
     */
    @Override
    public Sofa getSofa( String id)  {
        return connector.get(Sofa.class, id);
    }

    /**
     * 更新文档，id，rev为必须字段
     * @param sofa
     */
    @Override
    public Sofa updateSofa(Sofa sofa) {
        connector.update(sofa);
        return connector.get(Sofa.class,sofa.getId());
    }

    /**
     * 删除文档，_id，rev为必须字段
     * @param sofa
     */
    @Override
    public void deleteSofa(Sofa sofa) {
        connector.delete(sofa);
    }

}
